
#import "InfoTocViewController.h"

@interface InfoTocViewController () {
    NSArray *tableOfContents;
    DocumentViewController *documentVC;
}

@end

@implementation InfoTocViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
	NSString *path = [[NSBundle mainBundle] pathForResource:@"InfoToc" ofType:@"plist"];
	tableOfContents = [NSDictionary dictionaryWithContentsOfFile:path][@"toc"];
    
    documentVC = [[DocumentViewController alloc] initWithNibName:nil bundle:nil];
    for (NSDictionary *item in tableOfContents) {
        [documentVC showDocumentWithName:item[@"document"]];
    }
}

-(NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

#pragma mark - Table view stuff
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return tableOfContents.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *kCellIdentifier = @"InfoTocCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:17];
    }
	cell.tag = indexPath.row;
    NSDictionary *item = tableOfContents[indexPath.row];
    cell.textLabel.text = item[@"title"];
    cell.imageView.image = [UIImage imageNamed:item[@"icon"]];
    
	return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self informationCellPressedWithTag:indexPath.row];
}

#pragma mark - Information cell delegate
- (void)informationCellPressedWithTag:(NSInteger)tag
{
    NSDictionary *item = tableOfContents[tag];
    
    [documentVC showDocumentWithName:item[@"document"]];
    documentVC.backButtonTitle =  self.navigationItem.title;
    documentVC.title = item[@"title"];
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] 
                                   initWithTitle:@"Info"
                                   style: UIBarButtonItemStyleBordered
                                   target: nil action: nil];
    [self.navigationItem setBackBarButtonItem: backButton];
    [self.navigationController pushViewController:documentVC animated:YES];
}

@end
